﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace BLL.backStage
{
   public class UserManage
    {
       public static Boolean addUser(Dictionary<int, string> userdic)
        {
            Boolean flag = true;
            IDAL.IUserDAL userDAL = DALFactory.UserDALFactory.CreatInstance("SQLServerUserDAL");
            UserDetail user;
            user = new UserDetail();
            user.UserDetail_name = userdic[0];
            user.UserDetail_pwd = userdic[1];
            if (userdic[2].Equals("男"))
            {
                user.UserDetail_sex = true;
            }
            else
            {
                user.UserDetail_sex = false;
            }
            user.UserDetail_email = userdic[3];
            user.UserDetail_birth = userdic[4];
            user.UserDetail_phone = userdic[5];
            user.UserDetail_nation = userdic[6];
            user.UserDetail_province = userdic[7];
            user.UserDetail_city = userdic[8];
            user.UserDetail_regDate = userdic[9];
            //user.UserDetail_state = Int32.Parse( userdic[10]);
            user.UserDetail_memo = userdic[11];
            user.UserDetail_safeQue = userdic[12];
            user.UserDetail_safeAns = userdic[13];
            flag = userDAL.AddUser(user);
            return flag;
        }

       public static Boolean delUser(int id)
       {
           Boolean flag = true;
           IDAL.IUserDAL userDAL = DALFactory.UserDALFactory.CreatInstance("SQLServerUserDAL");
           flag = userDAL.DelUserById(id);
           return flag;
       }

       public static Boolean updUser(int id, Dictionary<int, string> userdic)
       {
           Boolean flag = true;
           IDAL.IUserDAL userDAL = DALFactory.UserDALFactory.CreatInstance("SQLServerUserDAL");
           UserDetail user;
           user = new UserDetail();
           user.UserDetail_name = userdic[0];
           user.UserDetail_pwd = userdic[1];
           if (userdic[2].Equals("True"))
           {
               user.UserDetail_sex = true;
           }
           else
           {
               user.UserDetail_sex = false;
           }
           user.UserDetail_email = userdic[3];
           user.UserDetail_birth = userdic[4];
           user.UserDetail_phone = userdic[5];
           user.UserDetail_nation = userdic[6];
           user.UserDetail_province = userdic[7];
           user.UserDetail_city = userdic[8];
           user.UserDetail_regDate = userdic[9];
           user.UserDetail_state = Int32.Parse(userdic[10]);
           user.UserDetail_memo = userdic[11];
           user.UserDetail_safeQue = userdic[12];
           user.UserDetail_safeAns = userdic[13];
           flag = userDAL.UpdUser(id,user);
           return flag;
       }

       public static int FillSearchTable(Table table,String userName,int pageNo)
       {
           IDAL.IUserDAL userDAL = DALFactory.UserDALFactory.CreatInstance("SQLServerUserDAL");
           List<UserDetail> userlist = userDAL.selectUserByName(userName);
           int beginItemNo = (pageNo - 1) * 5;
           int endItemNo = ((pageNo - 1) * 5 + 5) < (userlist.Count) ? ((pageNo - 1) * 5 + 5) : (userlist.Count);
           for (int i = beginItemNo ; i < endItemNo; ++i)
            {
                UserDetail user = userlist[i];

                TableRow tr = new TableRow();
                tr.BackColor = System.Drawing.Color.White;
                TableCell[] tc = new TableCell[6];

                for (int j = 0; j != 6; ++j)
                {
                    tc[j] = new TableCell();
                }

                tc[0].BackColor = System.Drawing.Color.White;
                tc[0].Text = user.User_id.ToString();
                tc[0].Height = 20;
                tc[0].VerticalAlign = VerticalAlign.Middle;
                tc[0].HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tc[0]);


                tc[1].BackColor = System.Drawing.Color.White;
                tc[1].Text = "<a href='./showUser.aspx?userId=" + user.User_id + "'>" + user.UserDetail_name + "</a>";
                tc[1].VerticalAlign = VerticalAlign.Middle;
                tc[1].HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tc[1]);


                tc[2].BackColor = System.Drawing.Color.White;
                tc[2].Text = user.UserDetail_phone;
                tc[2].VerticalAlign = VerticalAlign.Middle;
                tc[2].HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tc[2]);


                tc[3].BackColor = System.Drawing.Color.White;
                tc[3].Text = "<a href='./showUser.aspx?userId="+user.User_id+"'>查看|</a><a href='./updateUser.aspx?userId="+user.User_id+"'>编辑|</a><a href='./delUser.aspx?userId="+user.User_id+"'>删除</a>";
                tc[3].VerticalAlign = VerticalAlign.Middle;
                tc[3].HorizontalAlign = HorizontalAlign.Center; 
                tr.Cells.Add(tc[3]);
          
                
                table.Rows.Add(tr);
            }
            TableRow pageNoTr = new TableRow();
            TableCell pageNoTc = new TableCell();
            //pageNoTc.BackColor = System.Drawing.Color.White;
            int TotalPageNum = (userlist.Count / 5 + 1);
            String pageNoStr = "第" + pageNo + " / " + TotalPageNum + " 页";
            for (int j = 0; j < TotalPageNum; ++j)
            {
                if (j + 1 != pageNo)
                {
                   // pageNoStr += "<a href='./searchResult.aspx?keyType=" + userName + "&keyWord=" + keyWord + "&pageNo=" + (j + 1) + "'>[" + (j + 1) + "]</a>&nbsp;";
                }
                else
                {
                    pageNoStr += " " + (j + 1) + " ";
                }
            }
            pageNoTc.HorizontalAlign = HorizontalAlign.Center;
            pageNoTc.Text = pageNoStr;
            pageNoTr.Cells.Add(pageNoTc);
            table.Rows.Add(pageNoTr);

            return userlist.Count;
       }

       public static Dictionary<int, string> getUserById(int id)
       {
           Dictionary<int, string> user = new Dictionary<int, string>();
           UserDetail userdetail = new UserDetail();
           IDAL.IUserDAL userDAL = DALFactory.UserDALFactory.CreatInstance("SQLServerUserDAL");
           userdetail = userDAL.selectUserById(id);
           int i = 1;
           user.Add(i++, userdetail.UserDetail_name);
           user.Add(i++, userdetail.UserDetail_pwd);
           user.Add(i++, userdetail.UserDetail_sex.ToString());
           user.Add(i++, userdetail.UserDetail_email);
           user.Add(i++, userdetail.UserDetail_birth);
           user.Add(i++, userdetail.UserDetail_phone);
           user.Add(i++, userdetail.UserDetail_nation);
           user.Add(i++, userdetail.UserDetail_province);
           user.Add(i++, userdetail.UserDetail_city);
           user.Add(i++, userdetail.UserDetail_regDate);
           user.Add(i++, userdetail.UserDetail_state.ToString());
           user.Add(i++, userdetail.UserDetail_memo);
           user.Add(i++, userdetail.UserDetail_safeQue);
           user.Add(i++, userdetail.UserDetail_safeAns);

           user.Add(i++, userdetail.UserDetail_name);
           user.Add(i++, userdetail.UserDetail_name);
           user.Add(i++, userdetail.UserDetail_name);
           user.Add(i++, userdetail.UserDetail_name);
           return user;

       }

    }
}
